package com.holyant.goods.dto;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.core.annotation.Excel;
import com.ruoyi.common.core.domain.BaseDto;
import com.holyant.goods.enums.SpecsQtyEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.Objects;

/**
 * 商品当前价格dto
 * 
 * @author fkmalls
 * @date 2022-02-10
 */
@Data
@ApiModel("商品当前价格")
public class GdsSkuPriceDto extends BaseDto {

    private static final long serialVersionUID = 1L;

    /** 报价类型(10指导价20结算价30采购价) */
    @Excel(name = "报价类型(10指导价20结算价30采购价)")
    @ApiModelProperty(value = "报价类型(10指导价20结算价30采购价)")
    private Integer quoteType;
    /** 编码 */
    @Excel(name = "编码")
    @ApiModelProperty(value = "编码")
    private String code;
    /** 企业id */
    @Excel(name = "企业id")
    @ApiModelProperty(value = "企业id")
    private Long enterpriseId;
    /** 企业名称 */
    @Excel(name = "企业名称")
    @ApiModelProperty(value = "企业名称")
    private String enterpriseName;

    /**
     * 平台
     */
    private String platform;
    /** 渠道 */
    @Excel(name = "渠道")
    @ApiModelProperty(value = "渠道")
    private Integer salesChannel;
    /** 报价方式 10：包邮不包税，20：包邮包税，30: 不包邮不包税 */
    @Excel(name = "报价方式 10：包邮不包税，20：包邮包税，30: 不包邮不包税")
    @ApiModelProperty(value = "报价方式 10：包邮不包税，20：包邮包税，30: 不包邮不包税")
    private Integer quoteMethod;
    /** skuid */
    @Excel(name = "skuid")
    @ApiModelProperty(value = "skuid")
    private Long skuId;
    /** sku编码 */
    @Excel(name = "sku编码")
    @ApiModelProperty(value = "sku编码")
    private String skuCode;
    /** sku名称 */
    @Excel(name = "sku名称")
    @ApiModelProperty(value = "sku名称")
    private String skuName;
    /** 商品id */
    @Excel(name = "商品id")
    @ApiModelProperty(value = "商品id")
    private Long goodsId;
    /** 商品名称 */
    @Excel(name = "商品名称")
    @ApiModelProperty(value = "商品名称")
    private String goodsName;
    /** 商品条码 */
    @Excel(name = "商品条码")
    @ApiModelProperty(value = "商品条码")
    private String goodsBarcode;
    /** 商品编码 */
    @Excel(name = "商品编码")
    @ApiModelProperty(value = "商品编码")
    private String goodsCode;
    /** 分类id */
    @Excel(name = "分类id")
    @ApiModelProperty(value = "分类id")
    private Long categoryId;
    /**  分类编码 */
    @Excel(name = " 分类编码")
    @ApiModelProperty(value = " 分类编码")
    private String categoryCode;
    /** 分类名称 */
    @Excel(name = "分类名称")
    @ApiModelProperty(value = "分类名称")
    private String categoryName;
    /** 计量单位id */
    @Excel(name = "计量单位id")
    @ApiModelProperty(value = "计量单位id")
    private Long unitId;
    /** 计量单位名称 */
    @Excel(name = "计量单位名称")
    @ApiModelProperty(value = "计量单位名称")
    private String unitName;
    /** 毛重 */
    @Excel(name = "毛重")
    @ApiModelProperty(value = "毛重")
    private BigDecimal grossWeigtht;
    /** 净重 */
    @Excel(name = "净重")
    @ApiModelProperty(value = "净重")
    private BigDecimal netWeigtht;
    /** 体积 */
    @Excel(name = "体积")
    @ApiModelProperty(value = "体积")
    private BigDecimal volume;
    /** 规格 */
    @Excel(name = "规格")
    @ApiModelProperty(value = "规格")
    private String specs;
    /** 失效日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "失效日期", width = 30, dateFormat = "yyyy-MM-dd")
    @ApiModelProperty(value = "失效日期")
    private LocalDate expireDate;
    /** 箱规 */
    @Excel(name = "箱规")
    @ApiModelProperty(value = "箱规")
    private String boxSpec;
    /** 托规 */
    @Excel(name = "托规")
    @ApiModelProperty(value = "托规")
    private String palletSpec;
    /** 品牌id */
    @Excel(name = "品牌id")
    @ApiModelProperty(value = "品牌id")
    private Long brandId;
    /** 品牌 */
    @Excel(name = "品牌")
    @ApiModelProperty(value = "品牌")
    private String brandName;
    /** $column.columnComment */
    @Excel(name = "品牌")
    @ApiModelProperty(value = "品牌")
    private String goodsExtAttr;
    /** 币种 */
    @Excel(name = "币种")
    @ApiModelProperty(value = "币种")
    private Integer curType;
    /** 采购价格 */
    @Excel(name = "采购价格")
    @ApiModelProperty(value = "采购价格")
    private BigDecimal price;
    /** 价格json */
    @Excel(name = "价格json")
    @ApiModelProperty(value = "价格json")
    private String priceObj;
    /** 规格数量 */
    @Excel(name = "规格数量")
    @ApiModelProperty(value = "规格数量")
    private Integer specsQty;
    /** 生效日期 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "生效日期", width = 30, dateFormat = "yyyy-MM-dd")
    @ApiModelProperty(value = "生效日期")
    private LocalDate effectiveDate;
    @ApiModelProperty(value = "失效时间日期")
    private LocalDate effectiveTimeEnd;
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "实际生效时间日期")
    private LocalDate actualEffectiveTime;

    /** 停用标签 */
    @Excel(name = "停用标签")
    @ApiModelProperty(value = "停用标签")
    private String closeFlag;
    /** 描述 */
    @Excel(name = "描述")
    @ApiModelProperty(value = "描述")
    private String remark;
    /** 版本号 */
    @Excel(name = "版本号")
    @ApiModelProperty(value = "版本号")
    private Long ver;
    /** 店铺id */
    @ApiModelProperty(name = "店铺id")
    private Long shopId;
    /** 店铺名称 */
    @ApiModelProperty(name = "店铺名称")
    private String shopName;
    /** 客户id */
    @ApiModelProperty(name = "客户id")
    private Long customerId;
    /** 客户名称 */
    @ApiModelProperty(name = "客户名称")
    private String customerName;




    private Integer isCostPrice;


    private String delFlag;

    public JSONObject getSpecsPrice() {
        if (Objects.nonNull(priceObj)) {
            JSONObject priceObjJson = JSON.parseObject(priceObj);
            for (SpecsQtyEnum specsQtyEnum : SpecsQtyEnum.values()) {
                String priceStr = priceObjJson.getString(specsQtyEnum.name());
                if (!StrUtil.isEmpty(priceStr)){
                    //保留四位小数，四舍五入
                    BigDecimal price = new BigDecimal(priceStr).setScale(4, BigDecimal.ROUND_HALF_UP);
                    // 不足四位小数补0
                     priceStr = new DecimalFormat("0.0000#").format(price);
                    //重新put回 key值中
                    priceObjJson.put(specsQtyEnum.name(),new BigDecimal(priceStr));
                }
            }
            return priceObjJson;
        }
        return new JSONObject();
    }

    /** 根据汇率初始化价格json
     * @param bigDecimal
     */
    public void initializePriceObjByExChangeRate(BigDecimal bigDecimal){
        if (Objects.nonNull(priceObj)) {
            JSONObject priceObjJson = JSON.parseObject(priceObj);
            for (SpecsQtyEnum specsQtyEnum : SpecsQtyEnum.values()) {
                String priceStr = priceObjJson.getString(specsQtyEnum.name());
                if (!StrUtil.isEmpty(priceStr)){
                    //保留四位小数，四舍五入
                    BigDecimal price = new BigDecimal(priceStr).setScale(4, BigDecimal.ROUND_HALF_UP);
                    // 不足四位小数补0
                    priceStr = new DecimalFormat("0.0000#").format(price);
                    //重新put回 key值中
                    priceObjJson.put(specsQtyEnum.name(),new BigDecimal(priceStr).multiply(bigDecimal).setScale(4, BigDecimal.ROUND_HALF_UP));
                }
            }
            this.priceObj = JSON.toJSONString(priceObjJson);
        }
    }
}
